package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TFrontMenu;
import com.ruoyi.system.service.ITFrontMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前端菜单Controller
 *
 * @author kellen
 * @date 2024-04-17
 */
@Controller
@RequestMapping("/system/frontMenu")
public class TFrontMenuController extends BaseController {
    private String prefix = "system/frontMenu";

    @Autowired
    private ITFrontMenuService tFrontMenuService;

    @RequiresPermissions("system:frontMenu:view")
    @GetMapping()
    public String frontMenu() {
        return prefix + "/frontMenu";
    }

    /**
     * 查询前端菜单列表
     */
    @RequiresPermissions("system:frontMenu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TFrontMenu tFrontMenu) {
        startPage();
        List<TFrontMenu> list = tFrontMenuService.selectTFrontMenuList(tFrontMenu);
        return getDataTable(list);
    }

    /**
     * 导出前端菜单列表
     */
    @RequiresPermissions("system:frontMenu:export")
    @Log(title = "前端菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TFrontMenu tFrontMenu) {
        List<TFrontMenu> list = tFrontMenuService.selectTFrontMenuList(tFrontMenu);
        ExcelUtil<TFrontMenu> util = new ExcelUtil<TFrontMenu>(TFrontMenu.class);
        return util.exportExcel(list, "前端菜单数据");
    }

    /**
     * 新增前端菜单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存前端菜单
     */
    @RequiresPermissions("system:frontMenu:add")
    @Log(title = "前端菜单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TFrontMenu tFrontMenu) {
        return toAjax(tFrontMenuService.insertTFrontMenu(tFrontMenu));
    }

    /**
     * 修改前端菜单
     */
    @RequiresPermissions("system:frontMenu:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TFrontMenu tFrontMenu = tFrontMenuService.selectTFrontMenuById(id);
        mmap.put("tFrontMenu", tFrontMenu);
        return prefix + "/edit";
    }

    /**
     * 修改保存前端菜单
     */
    @RequiresPermissions("system:frontMenu:edit")
    @Log(title = "前端菜单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TFrontMenu tFrontMenu) {
        return toAjax(tFrontMenuService.updateTFrontMenu(tFrontMenu));
    }

    /**
     * 删除前端菜单
     */
    @RequiresPermissions("system:frontMenu:remove")
    @Log(title = "前端菜单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tFrontMenuService.deleteTFrontMenuByIds(ids));
    }


    @RequiresPermissions("system:frontMenu:view")
    @GetMapping("/getMenuTree")
    public String getMenuTree() {
        return prefix + "/tree";
    }
    /**
     * 树形菜单
     */
    @RequiresPermissions("system:frontMenu:list")
    @GetMapping("/getMenuTreeData")
    @ResponseBody
    public List<Ztree> getMenuTreeData() {
        return tFrontMenuService.queryMenuTree();
    }


}
